package com.mocne.MocneAutoTest;

import com.alibaba.fastjson.JSONObject;
import com.mocne.Common.ApiResponse;
import com.mocne.Common.PKFTestReport;
import com.mocne.TestCases.InterfaceTestTestNG_TestCase;
import com.mocne.TestCases.PortTestTestNG_TestCase;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
import org.springframework.web.bind.annotation.*;
import org.testng.TestNG;
import redis.clients.jedis.Jedis;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Collections;
import java.util.Properties;

/**
 * 这里的@RestController   相当于@ResponseBody + @Controller
 */
@RestController
@SpringBootApplication(exclude = {MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})
public class runCases {

    Properties properties = new Properties();
    FileInputStream inputStream;

    {
        try {
            inputStream = new FileInputStream("resource/RedisConfig.properties");
            properties.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    Jedis jedis = new Jedis(properties.getProperty("HOST"), Integer.parseInt(properties.getProperty("PORT")), Integer.parseInt(properties.getProperty("CONNECTION_TIME_OUT")), Integer.parseInt(properties.getProperty("SO_TIME_OUT")));


    @ResponseBody
    @RequestMapping(value = "/runCases", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public ApiResponse helloSpringBoot(@RequestBody JSONObject jsonParam) {

        ApiResponse response = new ApiResponse();

        if (null != jsonParam) {

            // 将获取的json数据封装一层，然后在给返回
            JSONObject result = new JSONObject();

            String dpFile = (String) jsonParam.get("dpFile");
            String caseSheet = (String) jsonParam.get("caseSheet");
            jedis.set("dpFile", dpFile);
            jedis.set("caseSheet", caseSheet);

            TestNG testNG = new TestNG();
            testNG.setVerbose(3);
            testNG.setThreadCount(3);
            testNG.setListenerClasses(Collections.singletonList(PKFTestReport.class));

            String[] reportFiles = dpFile.substring(0, dpFile.indexOf(".")).split("/");
            String reportFile = reportFiles[reportFiles.length - 1];

            jedis.set("projectName", reportFile);

            testNG.setOutputDirectory("/Users/pengkaifeng/Reports/" + reportFile + "_Report");//设置输出目录


            if ("portTest".equals(jsonParam.get("source"))) {

                testNG.setTestClasses(new Class[]{PortTestTestNG_TestCase.class});

            } else if ("interfaceTest".equals(jsonParam.get("source"))) {

                testNG.setTestClasses(new Class[]{InterfaceTestTestNG_TestCase.class});

            }

            testNG.run();

            try {
                result.put("report", "/Users/pengkaifeng/Reports/" + reportFile + File.separator + jedis.get("reportdate") + ".html");
            } catch (Exception e) {
                e.printStackTrace();
            }

            response.setSuccess(true);
            response.setCode(200);
            response.setMsg("Get params success.");
            response.setData(result);
            return response;
        }
        response.setSuccess(true);
        response.setCode(400);
        response.setMsg("Can not get params .");
        return response;
    }
}
